import tensorflow as tf
import os

'''
单输出感知机
'''
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

x = tf.random.normal([1, 3])
w = tf.ones([3, 1])
b = tf.ones([1])
y = tf.constant([1])
print('x: \n{}'.format(x.numpy()))
print('w: \n{}'.format(w.numpy()))
print('b: \n{}'.format(b.numpy()))
print('y: \n{}'.format(y.numpy()))

with tf.GradientTape() as tape:
    tape.watch([w, b])
    prob = tf.sigmoid(x @ w + b)
    loss = tf.reduce_mean(tf.losses.MSE(y, prob))

grad = tape.gradient(loss, [w, b])
print(grad)
